package com.shop.loan.proxy;

import com.shop.loan.mapper.LoanOrderPayLogByMybatisMapper;
import com.shop.loan.mapper.UserByMybatisMapper;
import com.shop.loan.model.LoanOrderPayLog;
import com.shop.loan.model.entity.LoanOrderPayLogByMybatis;
import com.shop.loan.model.entity.UserByMybatis;
import com.shop.loan.util.LoanOrderUtil;
import com.shop.loan.util.Util;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import xyz.erupt.annotation.fun.DataProxy;
import xyz.erupt.annotation.model.Column;
import xyz.erupt.annotation.model.Row;
import xyz.erupt.annotation.query.Condition;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Service
public class LoanOrderPayLogProxy implements DataProxy<LoanOrderPayLog> {

    @Resource
    LoanOrderPayLogByMybatisMapper loanOrderPayLogByMybatisMapper;

    @Resource
    UserByMybatisMapper userByMybatisMapper;

    @Resource
    LoanOrderUtil loanOrderUtil;

    @Resource
    HttpServletRequest httpServletRequest;

    @Resource
    Util util;

    @Override
    public String beforeFetch(List<Condition> conditions) {
        conditions.removeIf(condition -> "userInfo".trim().equals(condition.getKey()) || "userName".trim().equals(condition.getKey()) || "userPhone".trim().equals(condition.getKey()));
        List<Long> userIds = loanOrderUtil.getUserIdBySearch();
        String userIdStr = null;
        if (userIds != null && userIds.size() > 0) {
            userIdStr = org.apache.commons.lang3.StringUtils.join(userIds, ",");
        }
        String sql = null;
        String className = "LoanOrderPayLog";
        if (StringUtils.hasLength(userIdStr)) {
            sql = className + ".userId in (" + userIdStr + ")";
        }
        return sql;
    }


    @Override
    public void afterFetch(Collection<Map<String, Object>> list) {
        if (list == null || list.size() == 0) {
            return;
        }
        for (Map<String, Object> stringObjectMap : list) {
            UserByMybatis userByMybatis = userByMybatisMapper.selectByPrimaryKey(Long.valueOf(stringObjectMap.get("userId").toString()));
            if (userByMybatis != null) {
                stringObjectMap.put("userInfo", "姓名：" + userByMybatis.getCardName() + "\n 手机号：" + userByMybatis.getMobile());
            }
        }
    }

    @Override
    public List<Row> extraRow(List<Condition> conditions) {
        //行对象
        List<Row> rows = new ArrayList<>();

        String onlineStr = util.getSearchParam("online", 1);
        String paymentTypeStr = util.getSearchParam("paymentType", 1);
        String payTypeStr = util.getSearchParam("payType", 1);
        String orderIdStr = util.getSearchParam("orderId", 1);
        String payOrderIdStr = util.getSearchParam("payOrderId", 1);
        String createTimeStartStr = util.getSearchParam("createTime", 0);
        String createTimeEndStr = util.getSearchParam("createTime", 1);

        LoanOrderPayLogByMybatis search = new LoanOrderPayLogByMybatis();
        search.setUserIds(loanOrderUtil.getUserIdBySearch());
        if (StringUtils.hasLength(orderIdStr)) {
            search.setOrderId(Long.parseLong(orderIdStr));
        }
        if (StringUtils.hasText(createTimeStartStr)) {
            search.setStartTime(createTimeStartStr);
        }
        if (StringUtils.hasText(createTimeEndStr)) {
            search.setEndTime(createTimeEndStr);
        }
        if (StringUtils.hasText(onlineStr)) {
            search.setOnline(Boolean.valueOf(onlineStr));
        }
        if (StringUtils.hasText(paymentTypeStr)) {
            search.setPaymentType(Integer.valueOf(paymentTypeStr));
        }
        if (StringUtils.hasText(payTypeStr)) {
            search.setPayType(Integer.valueOf(payTypeStr));
        }
        if (StringUtils.hasText(payOrderIdStr)) {
            search.setPayOrderId(payOrderIdStr);
        }

        BigDecimal sumMoney = loanOrderPayLogByMybatisMapper.selectSum(search);
        sumMoney = sumMoney == null ? BigDecimal.ZERO : sumMoney;

        //列对象
        List<Column> totalColumns = new ArrayList<>();
        totalColumns.add(Column.builder().value("").colspan(1).build());
        totalColumns.add(Column.builder().value("总金额：" + sumMoney).colspan(1).className("text-red").build());

        rows.add(Row.builder().columns(totalColumns).build());
        return rows;
    }


}
